package com.version1_4.controller;

import com.alibaba.fastjson.JSONObject;
import com.util.aliyun.OCSutils;
import com.util.format.ReevesMap;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by reeveslee on 16/5/16.
 */
@Controller
public class OCSController {
    private static final Logger LOGGER = Logger.getLogger(OCSController.class);

    /**
     * 测试新建OCS键值对
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/ocs/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String ocsAdd(@RequestBody JSONObject jsonObject) {
        ReevesMap reevesMap = new ReevesMap();
        try {
            String key = (String) jsonObject.get("key");
            boolean result = new OCSutils().save(key, jsonObject, 600);
            reevesMap.put("result", result);
        } catch (Exception e) {
            reevesMap.setCode(500);
            reevesMap.setInfo("Error Occurred During Analyze The JsonObject");
            LOGGER.error(e);
        }
        return reevesMap.toJsonString();
    }

    /**
     * 测试获取OCS键值对
     *
     * @param key
     * @return
     */
    @RequestMapping(value = "/ocs/get/{key}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String ocsGet(@PathVariable("key") String key) {
        ReevesMap reevesMap = new ReevesMap();
        try {
            JSONObject jsonObject = (JSONObject) new OCSutils().get(key);
            reevesMap.put("result", jsonObject);
        } catch (Exception e) {
            reevesMap.setCode(500);
            reevesMap.setInfo("Error Occurred");
            LOGGER.error(e);
        }
        return reevesMap.toJsonString();
    }
}
